package com.mycc.game.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mycc.game.damin.Npc;
import com.mycc.game.damin.Rules;
import com.mycc.game.dao.RulesDao;
import com.mycc.game.service.RulesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RulesServiceImpl implements RulesService {
    @Autowired
    private RulesDao rules;

    //查询所有规则
    @Override
    public IPage<Rules> selAllRules(int pageNum, int pageSize) {
        Page<Rules> page = new Page<>(pageNum, pageSize);
        return rules.selectPage(page, null);
    }

    //根据id查询规则
    @Override
    public IPage<Rules> selAllRulesById(int id, int pageNum, int pageSize) {
        //创建查询容器
        LambdaQueryWrapper<Rules> qew = new LambdaQueryWrapper<>();
        qew.eq(Rules::getId, id);
        Page<Rules> page = new Page<>(pageNum, pageSize);
        //条件
        return rules.selectPage(page, qew);
    }

    //根据内容查询规则
    @Override
    public IPage<Rules> selAllRulesByContentByName(String content, int pageNum, int pageSize) {
        LambdaQueryWrapper<Rules> qew = new LambdaQueryWrapper<>();
        qew.like(Rules::getContent, content);
        Page<Rules> page = new Page<>(pageNum, pageSize);
        return rules.selectPage(page, qew);
    }


    //删除规则
    @Override
    public boolean delRulesById(int id) {
        int x = rules.deleteById(id);
        if (x > 0) {
            return true;
        } else {
            return false;
        }
    }

    //增加规则
    @Override
    public boolean addRules(Rules ruless) {
        int x = rules.insert(ruless);
        if (x > 0) {
            return true;
        } else {
            return false;
        }
    }

    //修改规则
    @Override
    public boolean updRules(int id, Rules ruless) {
        Rules rulesss = rules.selectById(id);
        if (rulesss != null) {
            BeanUtils.copyProperties(ruless, rulesss);
            int x = rules.updateById(rulesss);
            return x > 0;
        }
        return false;
    }
}
